package com.example.rbacmanage.config;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.util.RandomUtil;
import com.example.rbacmanage.common.Constant;
import com.example.rbacmanage.property.MinAppProperty;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAPublicKeyConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import java.io.File;
import java.io.InputStream;

@Configuration
public class WxConfig {

    @Autowired
    private MinAppProperty minAppProperty;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 微信支付配置
     */
    @Bean
    public  JsapiServiceExtension wxPayConfig(){
        ClassPathResource classPathResource = new ClassPathResource(minAppProperty.getPrivateKeyPath());
        InputStream inputStream = classPathResource.getStream();
        String privatePath;
        File file;
        if (redisTemplate.opsForValue().get(Constant.PRIVATE_PATH) != null){
            privatePath= String.valueOf(redisTemplate.opsForValue().get(Constant.PRIVATE_PATH));
            file = new File(privatePath);
            if (!file.exists()){
                privatePath = RandomUtil.randomNumbers(8)+".pem";
                redisTemplate.opsForValue().set(Constant.PRIVATE_PATH,privatePath);
                file = new File(privatePath);
                FileUtil.copyFile(inputStream,file);
            }
        }else{
            privatePath = RandomUtil.randomNumbers(8)+".pem";
            redisTemplate.opsForValue().set(Constant.PRIVATE_PATH,privatePath);
            file = new File(privatePath);
            FileUtil.copyFile(inputStream,file);
        }
        file.deleteOnExit();

        ClassPathResource classPathResource2 = new ClassPathResource(minAppProperty.getPublicKeyPath());
        InputStream inputStream2 = classPathResource2.getStream();
        String publicPath;
        File file2;
        if (redisTemplate.opsForValue().get(Constant.PUBLIC_PATH) != null){
            publicPath = String.valueOf(redisTemplate.opsForValue().get(Constant.PUBLIC_PATH));
            file2 = new File(publicPath);
            if (!file2.exists()){
                publicPath = RandomUtil.randomNumbers(8)+".pem";
                redisTemplate.opsForValue().set(Constant.PUBLIC_PATH,publicPath);
                file2 = new File(publicPath);
                FileUtil.copyFile(inputStream2,file2);
            }
        }else{
            publicPath = RandomUtil.randomNumbers(8)+".pem";
            redisTemplate.opsForValue().set(Constant.PUBLIC_PATH,publicPath);
            file2 = new File(publicPath);
            FileUtil.copyFile(inputStream2,file2);
        }
        file2.deleteOnExit();
        Config config =
                new RSAPublicKeyConfig.Builder()
                        .merchantId(minAppProperty.getMerchantId())
                        // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
                        .privateKeyFromPath(file.getAbsolutePath())
                        .publicKeyFromPath(file2.getAbsolutePath())
                        .publicKeyId(minAppProperty.getPublicKeyId())
                        .merchantSerialNumber(minAppProperty.getMerchantSerialNumber())
                        .apiV3Key(minAppProperty.getApiV3Key())
                        .build();
        return new JsapiServiceExtension.Builder().config(config).build();
    }
}
